#include "ClientWindow.h"

// Public Methods

ClientWindow::ClientWindow (QWidget * parent) : QMainWindow(parent)
{
    client = new PlaylistClient();

    setWindowTitle("SocialRadio - Client");
    setCentralWidget(new QWidget());
    resize(400, 400);

    initMenu();
    initDefaultLayout();
    initConnectedLayout();

    updateGUI();
}

void ClientWindow::notify (Observable * obj, int eventCode)
{

}

// Slots

void ClientWindow::connect ()
{
    client->connectToServer("127.0.0.1", 4998);
}

void ClientWindow::disconnect ()
{
    client->getPlaylist()->addSong(Song("Music 3.wav"));
}

void ClientWindow::recordAndUpload ()
{
    client->getPlaylist()->addSong(Song("Music2.wav"));
}

void ClientWindow::chooseFileAndUpload ()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Choose audio file to upload"), "~", tr("Audio (*.wav)"));
    client->sendFile(fileName.toStdString());
}

void ClientWindow::play ()
{

}

void ClientWindow::pause ()
{

}

void ClientWindow::stop ()
{

}

// Private Methods

void ClientWindow::initMenu ()
{
    QMenu * connectionMenu = menuBar()->addMenu("&Connection");
    QAction * connectAction = new QAction(tr("&Connect..."), this);
    QAction * disconnectAction = new QAction(tr("&Disconnect"), this);
    connectionMenu->addAction(connectAction);
    connectionMenu->addAction(disconnectAction);

    QMenu * uploadMenu = menuBar()->addMenu("&Upload");
    QAction * recordAction = new QAction(tr("&Record microphone..."), this);
    QAction * fileAction = new QAction(tr("&Send audio file..."), this);
    uploadMenu->addAction(recordAction);
    uploadMenu->addAction(fileAction);

    QObject::connect(connectAction, SIGNAL(triggered()), this, SLOT(connect()));
    QObject::connect(disconnectAction, SIGNAL(triggered()), this, SLOT(disconnect()));

    QObject::connect(recordAction, SIGNAL(triggered()), this, SLOT(recordAndUpload()));
    QObject::connect(fileAction, SIGNAL(triggered()), this, SLOT(chooseFileAndUpload()));
}

void ClientWindow::updateMenu ()
{
}

void ClientWindow::initDefaultLayout ()
{
    defaultLayout = new QVBoxLayout();
}

void ClientWindow::initConnectedLayout ()
{
    connectedLayout = new QVBoxLayout();
    playlistWidget = new PlaylistWidget(client->getPlaylist());
    playlistWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
    connectedLayout->addWidget(playlistWidget, Qt::AlignTop);

    playbackBar = new QToolBar;

    playAction = new QAction(style()->standardIcon(QStyle::SP_MediaPlay), tr("Play"), this);
    playAction->setDisabled(true);
    playbackBar->addAction(playAction);

    pauseAction = new QAction(style()->standardIcon(QStyle::SP_MediaPause), tr("Pause"), this);
    pauseAction->setDisabled(true);
    playbackBar->addAction(pauseAction);

    stopAction = new QAction(style()->standardIcon(QStyle::SP_MediaStop), tr("Stop"), this);
    stopAction->setDisabled(true);
    playbackBar->addAction(stopAction);

    connectedLayout->addWidget(playbackBar, Qt::AlignHCenter);
    connectedLayout->addStretch();
}

void ClientWindow::updateGUI ()
{
    centralWidget()->setLayout(connectedLayout);
}
